package com.wellsoft.mobileoa.utils.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.ComboListData;
import com.wellsoft.mobileoa.utils.SelectComboListAdapter;
import com.wellsoft.mobileoa.utils.SelectListDataAdapter;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Response;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSubItem;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.PopListDialog;
import com.wmobile.utils.ServiceResponse;

public class PopNewPageAction extends BaseAction {

	public PopNewPageAction(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		if (!SystemConfig.stringIsNullOrEmpty(action.getListfield())) {
			ServiceResponse response = baseView.checkValid();
			if (!response.getExeFlag()) {
				return response;
			}
		}

		WSOA_HttpGet_Request request = new WSOA_HttpGet_Request();
		request.setUrl(action.getUrl());
		request.setCookies(SystemConfig.getCookies());

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);
		ServiceResponse httpGetResponse = SystemConfig.getService()
				.Callservice(httpRequests, "WSOA_HttpGet",
						WSOA_HttpGet_Response.class);
		if (!httpGetResponse.getExeFlag()) {
			return httpGetResponse;
		}
		WSOA_HttpGet_Response response = (WSOA_HttpGet_Response) httpGetResponse
				.getResponseData();
		if (response.getExeFlag() == 2) {
			if (SystemConfig.reLogin()) {
				return excuteAction();
			} else {
				BaseActivity.returnLoginActivity();
				return new ServiceResponse(false);
			}
		}
		if (response.getExeFlag() == 0) {
			return new ServiceResponse(false, response.getMessage());
		}

		PopListDialog popDialog;
		List<Integer> selectedIndex;
		BaseWidget sender;
		BaseWidget hiddenSender;
		String resultValue = "";
		String hiddenResultValue = "";
		boolean isMultiSelect;
		if (response.getNextPage().getPageType().equals("g")) {
			isMultiSelect = false;
		} else {
			isMultiSelect = true;
		}

		List<ComboListData> dataList = new ArrayList<ComboListData>();
		selectedIndex = new ArrayList<Integer>();
		sender = baseView.getWidgetById(action.getPname());
		hiddenSender = baseView.getWidgetById(action.getPhname());
		String initValue = sender.getRawValue();

		if (response.getNextPage().getPageListSet() != null
				&& response.getNextPage().getPageListSet().getListItems() != null) {
			for (int i = 0; i < response.getNextPage().getPageListSet()
					.getListItems().size(); i++) {
				WSOA_PageListItem listItem = response.getNextPage()
						.getPageListSet().getListItems().get(i);
				if (listItem.getListSubItems() != null) {
					for (int j = 0; j < listItem.getListSubItems().size(); j++) {
						WSOA_PageListSubItem subItem = listItem
								.getListSubItems().get(j);
						if (subItem.getValue() == null)
							continue;
						ComboListData listData = new ComboListData();
						listData.setMessage(listItem.getMessage() + "\\"
								+ subItem.getMessage());
						listData.setValue(subItem.getValue());
						listData.setHiddenValue(subItem.getHiddenValue());

						dataList.add(listData);
					}
				}
				if (listItem.getValue() == null)
					continue;
				ComboListData listData = new ComboListData();
				listData.setMessage(listItem.getMessage());
				listData.setValue(listItem.getValue());
				listData.setHiddenValue(listItem.getHiddenValue());

				dataList.add(listData);
			}
		}

		if (!SystemConfig.stringIsNullOrEmpty(initValue)) {
			if (!SystemConfig.stringIsNullOrEmpty(sender.getField()
					.getSeparator())) {
				String[] strArr = initValue.split(sender.getField()
						.getSeparator());
				for (String str : strArr) {
					int index = findIndexInComboList(str, dataList);
					if (index > -1) {
						selectedIndex.add(Integer.valueOf(index));
					}
				}
			} else {
				int index = findIndexInComboList(initValue, dataList);
				if (index > -1) {
					selectedIndex.add(Integer.valueOf(index));
				}
			}
		}
		SelectComboListAdapter dataAdapter = new SelectComboListAdapter(
				baseView.getBaseActivity(), dataList, selectedIndex,
				isMultiSelect);
		if (action.getPageTitle() == null
				|| SystemConfig.stringIsNullOrEmpty(action.getPageTitle()
						.getMessage())) {
			popDialog = new PopListDialog<ComboListData>(
					baseView.getBaseActivity(), response.getNextPage().getPageTitle().getMessage(), selectedIndex,
					dataAdapter, isMultiSelect);
		} else {
			popDialog = new PopListDialog<ComboListData>(
					baseView.getBaseActivity(), action.getPageTitle()
							.getMessage(), selectedIndex, dataAdapter,
					isMultiSelect);
		}
		if (popDialog.showDialog() != PopListDialog.DialogResultOK) {
			return new ServiceResponse(false);
		}
		selectedIndex = popDialog.getSelectedIndex();
		for (Integer i : selectedIndex) {
			if (!SystemConfig.stringIsNullOrEmpty(sender.getField()
					.getSeparator())) {
				resultValue += dataList.get(i).getValue()
						+ sender.getField().getSeparator();
			} else {
				resultValue += dataList.get(i).getValue();
			}
			if (hiddenSender != null) {
				if (!SystemConfig.stringIsNullOrEmpty(hiddenSender.getField()
						.getSeparator())) {
					hiddenResultValue += dataList.get(i).getHiddenValue()
							+ hiddenSender.getField().getSeparator();
				} else {
					hiddenResultValue += dataList.get(i).getHiddenValue();
				}
			}
		}
		if (!SystemConfig.stringIsNullOrEmpty(resultValue)
				&& !SystemConfig.stringIsNullOrEmpty(sender.getField()
						.getSeparator())) {
			resultValue = resultValue.substring(0, resultValue.length()
					- sender.getField().getSeparator().length());
		}
		if (hiddenSender != null
				&& !SystemConfig.stringIsNullOrEmpty(hiddenResultValue)
				&& !SystemConfig.stringIsNullOrEmpty(hiddenSender.getField()
						.getSeparator())) {
			hiddenResultValue = hiddenResultValue.substring(0,
					hiddenResultValue.length()
							- hiddenSender.getField().getSeparator().length());
		}
		sender.setValue(resultValue);
		if (hiddenSender != null) {
			hiddenSender.setValue(hiddenResultValue);
		}
		if (action.getNextAction() != null) {
			ServiceResponse serviceResponse = BaseAction.executeActions(
					action.getNextAction(), null, baseView);
			return serviceResponse;
		}
		return new ServiceResponse(true);
	}

	private int findIndexInComboList(String value, List<ComboListData> dataList) {
		if (SystemConfig.stringIsNullOrEmpty(value))
			return -1;
		for (int i = 0; i < dataList.size(); i++) {
			if (dataList.get(i).getValue().equals(value))
				return i;
		}
		return -1;
	}

}
